package lib.bone.admin.controller.flow;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lib.bone.admin.anno.WebLog;
import lib.bone.admin.constant.MethodTypeConst;
import lib.bone.flow.domain.entity.FlowTaskAssign;
import lib.bone.flow.domain.dto.FlowTaskAssignAddDTO;
import lib.bone.flow.domain.dto.FlowTaskAssignQueryDTO;
import lib.bone.flow.service.FlowTaskAssignService;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.ResponseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @author: kindear
 */

@RestController
@RequestMapping("flow")
@Api(tags = "流程审核节点")
public class FlowTaskAssignController {
    @Autowired
    FlowTaskAssignService flowTaskAssignService;

    @WebLog(module = "bone-flow")
    @PostMapping("assign")
    @ApiOperation(value = "新增审核指派")
    public ResponseDTO<FlowTaskAssign> newAssign(@RequestBody FlowTaskAssignAddDTO dto){
        return flowTaskAssignService.saveOne(dto);
    }

    /**
     * 查询审核指派
     */
    @GetMapping("assign/{id}")
    @ApiOperation(value = "查询单个审核指派")
    public ResponseDTO<FlowTaskAssign> getAssign(@PathVariable("id") String id){
        Optional<FlowTaskAssign> assignOp = flowTaskAssignService.getById(id);
        return assignOp.map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND));
    }

    /**
     * 条件查询审核指派
     */
    @GetMapping("assign")
    @ApiOperation(value = "查询审核指派列表")
    public ResponseDTO getAssignList(FlowTaskAssignQueryDTO dto){
        Sort sort;
        if(dto.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, dto.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, dto.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(dto.getPageNum() - 1 , dto.getPageSize(), sort);

        FlowTaskAssign assign = new FlowTaskAssign();
        BeanUtil.copyProperties(dto,assign, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<FlowTaskAssign> example = Example.of(assign);

        //return nepuPaperDictService.getAll(pageable,example);
        return ResponseDTO.succData(flowTaskAssignService.getAll(pageable,example));
    }


    /**
     * 审核指派修改
     */
    @WebLog(module = "bone-flow",operate = MethodTypeConst.UPDATE)
    @PutMapping("assign")
    @ApiOperation(value = "更新审核指派")
    public ResponseDTO<FlowTaskAssign> updateAssign(){
        return ResponseDTO.wrap(ResponseCodeConst.DEVELOPMENT);
       // return null;
    }

    @WebLog(module = "bone-flow",operate = MethodTypeConst.DELETE)
    @DeleteMapping("assign/{id}")
    @ApiOperation(value = "删除审核指派")
    public ResponseDTO deleteAssign(@PathVariable("id") String id){
        flowTaskAssignService.deleteAssign(id);
        return ResponseDTO.succMsg("删除成功");
    }
}
